package com.mission.game.components;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mission.game.R;
import com.mission.game.otherscreens.MainScreen;

public class InviteAdapter extends ArrayAdapter<String> {
	Context _context;
	int _layoutResourceId;
	ArrayList<String> _invitations;

	public InviteAdapter(Context context, int layoutResourceId,
			ArrayList<String> invitations) {
		super(context, layoutResourceId, invitations);
		_layoutResourceId = layoutResourceId;
		_context = context;
		_invitations = invitations;
	}

	static class InviteHolder {
		ImageView checkIcon;
		ImageView crossIcon;
		TextView inviteTxt;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		InviteHolder holder = null;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.game_invite_item, null);

			holder = new InviteHolder();
			holder.checkIcon = (ImageView) v.findViewById(R.id.invite_check);
			holder.crossIcon = (ImageView) v.findViewById(R.id.invite_cross);
			holder.inviteTxt = (TextView) v.findViewById(R.id.invite_text);

			holder.crossIcon.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					((MainScreen)_context).removeMissionItem(position);
				}
			});

			holder.checkIcon.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					((MainScreen)_context).addMissionItem(position);
				}
			});

			holder.inviteTxt.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					((MainScreen)_context).showMissionData(position);
				}
			});

			
			v.setTag(holder);
		} else {
			holder = (InviteHolder) v.getTag();
		}
		holder.inviteTxt.setText(_invitations.get(position));
		return v;
	}
}